package com.xxx.sso.util;

import com.xxx.sso.consts.PageableConst;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.Optional;

/**
 * 分页工具类
 * @author caiwl
 * @date 2020/5/6 11:13
 */
@Deprecated
public class PageUtil {
    public static Pageable of(Integer page, Integer size, String... sort) {
        if (page == null) {
            page = PageableConst.DEFAULT_PAGE;
        }
        size = Optional.ofNullable(size).orElse(PageableConst.DEFAULT_SIZE);
        if (StringUtil.isEmpty(sort[0])) {
            sort[0] = PageableConst.DEFAULT_SORT;
        }
        String[] sortArr = sort[0].split(",");
        Sort.Direction direction = sortArr.length == 1 ? Sort.DEFAULT_DIRECTION : Sort.Direction.fromString(sortArr[1]);
        return PageRequest.of(page, size, Sort.by(direction, sortArr[0]));
    }

    public static void main(String[] args) {
        Integer page = null;
//        if (page == null) {
//            page = PageableConst.DEFAULT_PAGE;
//        }
        page = Optional.ofNullable(page).orElse(PageableConst.DEFAULT_PAGE);
        System.out.println(page);
    }
}
